package com.example.timeplanning_teacher;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import org.json.JSONObject;
import java.sql.ResultSet;

public class MainActivity extends AppCompatActivity {

    private TextView tx_signup;
    private TextView tx_forget;
    private EditText et_username;
    private EditText et_password;
    private Handler handler;

    private String username;
    private String password;

    Intent intent1;

    private SharedPreferences loginPreference;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        tx_signup = findViewById(R.id.tx_signup);
        tx_forget = findViewById(R.id.tx_forget);
        et_username = findViewById(R.id.et_usr);
        et_password = findViewById(R.id.et_psd);

        loginPreference = getSharedPreferences("parent", Context.MODE_PRIVATE);
        et_username.setText(loginPreference.getString("username",""));
        et_password.setText(loginPreference.getString("password",""));

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        Intent intent=new Intent(MainActivity.this, FragmentActivity.class);
                        startActivity(intent);
                        MainActivity.this.finish();
                }
            };
        };

        setListener();
    }
    private void setListener(){
        OnClick onClick = new OnClick();
        tx_signup.setOnClickListener(onClick);
        tx_forget.setOnClickListener(onClick);
    }

    private class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch(v.getId()){
                case R.id.tx_signup:
                    Bundle bundle = new Bundle();
                    intent =new Intent(MainActivity.this, CaptchaActivity.class);
                    String name = "toRegister";
                    bundle.putString("name",name);
                    intent.putExtra("Message",bundle);

                    break;
                case R.id.tx_forget:
                    intent =new Intent(MainActivity.this,ForgetActivity.class);

                    break;
            }
            startActivity(intent);
        }
    }

    public void signin(View v){
        username = et_username.getText().toString();
        password = et_password.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String json = "{\n" +
                            "  \"username\": \"" + username + "\",\n" +
                            "  \"password\": \"" + password + "\"\n" +
                            "}";
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://58.58.36.198:9900/parent/login")
                            .post(RequestBody.create(MediaType.parse("application/json"),json))
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    JSONObject jsonObject=new JSONObject(responseData);
                    int code=(int)jsonObject.get("code");
                    if(code == 100){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                SharedPreferences.Editor editor = loginPreference.edit();
                                editor.putString("username", et_username.getText().toString());
                                editor.putString("password", et_password.getText().toString());
                                editor.commit();
                                Message message = new Message();
                                message.what = 1;
                                handler.sendMessage(message);
                                Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else if(0 == code){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else if(1 == code){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"用户名不存在",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }


}